from ..extensions import db
from datetime import datetime

class Complaint(db.Model):
    __tablename__ = 'complaints'
    
    id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"), primary_key=True, autoincrement=True, comment='投诉ID')
    user_id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"), nullable=False, comment='投诉用户ID，逻辑外键 users.id')
    complaint_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow, comment='投诉时间')
    content = db.Column(db.Text, nullable=False, comment='投诉内容')
    images = db.Column(db.Text, nullable=False, comment='投诉图片，多张可用逗号分隔存储')
    upvotes = db.Column(db.Integer, nullable=False, default=0, comment='点赞数')
    downvotes = db.Column(db.Integer, nullable=False, default=0, comment='点踩数')
    created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow, comment='记录创建时间')
    
    def to_dict(self):
        # 将图片字符串转换为列表（如果是逗号分隔的多张图片）
        images_list = []
        if self.images:
            images_list = [img.strip() for img in self.images.split(',')] if ',' in self.images else [self.images.strip()]
        
        return {
            'id': self.id,
            'user_id': self.user_id,
            'complaint_time': str(self.complaint_time),
            'content': self.content,
            'images': images_list,  # 返回图片列表而不是原始字符串
            'upvotes': self.upvotes,
            'downvotes': self.downvotes,
            'created_at': str(self.created_at)
        }